// pages/user_center/proxypage/proxypage.js
var Tool = require("../../../utils/ztool.js");
var tool = new Tool();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    userphone: '',
    address: '',
    introduction: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo)

    switch (userInfo.USerType) {
      case '1': {
        this.setData({
          username: userInfo.CompanyName,
          userphone: userInfo.CompanyNumber
        })
        break
      }

      case '2': {
        this.setData({
          username: userInfo.UCompanyName,
          userphone: userInfo.UCompanyNumber
        })
        break
      }

      case '5': {
        this.setData({
          username: userInfo.cName,
          userphone: userInfo.cTel
        })
        break
      }

      default: {
        this.setData({
          username: userInfo.sName,
          userphone: userInfo.sTel
        })
        break
      }
    }


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  callphone: function (e) {
    var that = this
    console.log(e)
    var phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },

  submit: function () {
    var that = this
    var useinfo = wx.getStorageSync('userInfo')
    console.log(useinfo)

    if (that.data.address.length == 0) {
      wx.showToast({
        title: '请输入地址',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    if (that.data.introduction.length == 0) {
      wx.showToast({
        title: '请输入情况简介',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    var type = ''
    var tel = ''
    var pwd = ''

    switch (useinfo.USerType) {
      case '1': {
        type = useinfo.USerType
        tel = useinfo.Telphone
        pwd = wx.getStorageSync('password')
        break
      }

      case '2': {
        type = useinfo.USerType
        tel = useinfo.Telphone
        pwd = wx.getStorageSync('password')
        break
      }

      case '5': {
        type = useinfo.USerType
        tel = useinfo.cTel
        pwd = wx.getStorageSync('password')
        break
      }

      default: {
        type = useinfo.USerType
        tel = useinfo.sTel
        pwd = wx.getStorageSync('password')
        break
      }
    }

    console.log(type)
    console.log(tel)
    console.log(pwd)

    wx.request({
      url: 'https://www.lanshuicar.com/Server/JoinApplyHdl.ashx',
      data: {
        tel: tel,
        pwd: pwd,
        act: 'add',
        type: type,
        ytel: that.data.userphone,
        yname: that.data.username,
        yprovince: that.data.address,
        ycity: that.data.address,
        ylmsg: that.data.introduction
      },
      method: 'GET',
      success: function (res) {
        var data = tool.getRegularJson(res.data);
        console.log(data)
        if (data.code == '0') {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '提交失败，请重试',
            icon: 'none',
            duration: 2000
          })
        }

      },
      fail: function () {
        console.log('加载错误');
      }
    })
  },

  inputname: function (e) {
    var that = this
    console.log(e)
    that.data.username = e.detail.value
    console.log(that.data.username)
  },

  inputphone: function (e) {
    var that = this
    console.log(e)
    that.data.userphone = e.detail.value
    console.log(that.data.userphone)
  },

  inputaddr: function (e) {
    var that = this
    console.log(e)
    that.data.address = e.detail.value
    console.log(that.data.address)
  },

  inputintroduction: function (e) {
    var that = this
    console.log(e)
    that.data.introduction = e.detail.value
    console.log(that.data.introduction)
  }
})